import axios from "axios";
import {generateSignature,getDate} from './ai';
const APPID='3399720a'
const API_Secret='YmUwZDI5MDIzNTMyMzg1NmYyZTY5OGU0'
const API_Key='20f0551e82d1528ed188fbf7048d3c0c'
const HOST='tts-api.xfyun.cn';

export const textToSpeech=async(text:string)=>{
    const date=getDate();
    const signature=generateSignature(API_Key,API_Secret,date,HOST);
    const url=`https://${HOST}/v2/tts`;
    try{
        const response=await axios.post(url,{
            common:{
                app_id:APPID
            },
            business:{
                aue:'lame',
                auf:'audio/L16;rate=16000',
                voice_name:'xiaoyan',
                speed:50,
                volume:50,
                pitch:50
            },
            data:{
                text:btoa(unescape(encodeURIComponent(text))),
                staus:2
            }
        },
        {
            headers:{
                'Content-Type':'application/json',
                'Host':HOST,
                'Date':date,
                'Authorization':signature
            },
            responseType:'arraybuffer'
        });
        const audioBlob=new Blob([response.data],{type:'audio/mp3'});
        const audioUrl=URL.createObjectURL(audioBlob);
        const audio=new Audio(audioUrl);
        audio.play();
    }catch(error){
        console.log('语音合成出错',error);
    }
}
